package com.andy.customerService.controller;

import com.andy.customerService.entity.JWTUserInfo;
import com.andy.customerService.enums.ExceptionEnum;
import com.andy.customerService.exception.BusinessException;
import com.andy.customerService.utils.JwtUtil;
import com.andy.customerService.vo.JsonBaseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Andy
 * @date 2022/2/22 16:06
 */
@RestController
@RequestMapping("admin/login")
@Slf4j
public class Login {

    @PostMapping
    public JsonBaseResult<Object> login(HttpServletRequest request){
        JsonBaseResult<Object> result = new JsonBaseResult<>();
        //若有token，校验是否正常
//        String token = request.getHeader("token");
//        if (StringUtils.isNoneBlank(token)) {
//            //根据token解析对象
//            JWTUserInfo jwtUserInfo = JwtUtil.getJWTUserInfoByToken(token);
//            if (jwtUserInfo == null) {
//                throw new BusinessException(ExceptionEnum.TOKEN_VALIDATE_ERROR);
//            }
//             result.setResult(token);
//             return result;
//        }
        //测试用user信息
        JWTUserInfo jwtUserInfo = new JWTUserInfo();
        jwtUserInfo.setLoginName("andy");
        jwtUserInfo.setPhone("111111");
        jwtUserInfo.setUserName("andy");
        jwtUserInfo.setEmail("1073742767@qq.com");
        jwtUserInfo.setId(1);
        jwtUserInfo.setRoleCode("admin");

        // 生成token
        String jwtToken = JwtUtil.getTokenByJWTUserInfo(jwtUserInfo);
        result.setResult(jwtToken);
        log.info("jwtToken "+jwtToken);
        return result;
    }
}
